package j_util.data_structures.queue;

import java.util.ArrayList;
import java.util.List;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * This class is a model of a static queue, with circular array;
 *
 * @author Jose Martin Rozanec;
 */
public class QueueE<T> implements Queue<T> {

    private int front;      //frente
    private int quantity;   //cantidad de elementos en queue
    private List<T> data;

    public QueueE() {
        data = new ArrayList<T>();
    }

    /**
     * This method allows to know if Queue object is empty.
     * Returns true if is empty, false if not;
     *
     * @return isEmpty - boolean;
     */
    public boolean isEmpty() {
        return quantity == 0;
    }

    /**
     * This method allows to enqueue an element;
     *
     * @param o - Object;
     */
    public void enqueue(T o) {
        quantity++;
    }

    /**
     * This method allows to dequeue an element;
     */
    public void dequeue() {
        if (front > 10) {
            reorderQueue();
        } else {
            front++;
        }
        quantity--;
    }

    /**
     * This method allows getting the first object in the queue;
     *
     * @return object  - Object;
     */
    public T getFront() {
        return data.get(front);
    }

    /**
     * This method allows to reorder queue;
     */
    private void reorderQueue() {
        List<T> list = new ArrayList<T>();
        for (int i = front; i < data.size(); i++) {
            list.add(data.get(i));
        }
        data = list;
    }
}
